/*
 * SwingSet.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.demos.swingset;

import java.util.ArrayList;
import java.util.HashMap;

import com.cute4j.swing.CheckBoxMenuItem;
import com.cute4j.swing.Container;
import com.cute4j.swing.Frame;
import com.cute4j.swing.Menu;
import com.cute4j.swing.MenuBar;
import com.cute4j.swing.MenuItem;
import com.cute4j.swing.SplashScreen;
import com.cute4j.swing.StatusBar;
import com.cute4j.swing.Swing;
import com.cute4j.swing.TabbedPane;
import com.cute4j.swing.ToggleToolButton;
import com.cute4j.swing.ToolBar;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.events.ActionListener;
import com.cute4j.swing.layouts.BorderLayout;
import com.cute4j.swingx.StyleMenu;
import com.cute4j.swingx.ThemeMenu;
import com.cute4j.swingx.actions.AboutQtAction;
import com.cute4j.swingx.text.code.CodeViewer;
import com.cute4j.swingx.text.code.JavaDocument;

/**
 * Cute4j SwingSet.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
final class SwingSet extends Frame {

// Private Fields. -------------------------------------------------------------

  private ToolBar toolBar;
  private TabbedPane tabbedPane;
  private StatusBar statusBar;
  private ArrayList demos;
  private DemoModule currentDemo;
  private Container demoPanel;
  private CodeViewer sourceCode;
  private final HashMap sourceCodeCache;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new SwingSet object.
   */
  public SwingSet() {
    super("Cute4j SwingSet");
    sourceCodeCache = new HashMap();
    setDefaultCloseOperation(QUIT_ON_CLOSE);
    setSize(720, 640);
    setLocationRelativeTo(null);
    buildMenuBar();
    buildToolBar();
    buildContent();
    buildStatusBar();
    addDemos();
    ((ToggleToolButton)toolBar.getComponent(0)).setSelected(true);
    setCurrentDemo(0);
    setDefaultResourceBundleName();
  }

// Package-Private Methods. ----------------------------------------------------

  void performToolBar() {
    toolBar.setVisible(!toolBar.isVisible());
  }

  void performStatusBar() {
    statusBar.setVisible(!statusBar.isVisible());
  }

  void setCurrentDemo(int index) {
    DemoModule demo = (DemoModule)demos.get(index);
    if (currentDemo != demo) { // NOPMD
      tabbedPane.setTitleAt(0, demo.getName());
      if (currentDemo != null) {
        demoPanel.remove(currentDemo.getComponent());
      }
      demoPanel.add(demo.getComponent());
      JavaDocument javaDocument = (JavaDocument)sourceCodeCache.get(demo);
      if (javaDocument == null) {
        javaDocument = new JavaDocument(demo.getSourceCode());
      }
      sourceCode.setDocument(javaDocument);
      currentDemo = demo;
    }
  }

// Private Methods. ------------------------------------------------------------

  private void buildMenuBar() {
    MenuBar menuBar = new MenuBar();
    Menu menu = menuBar.add(new Menu("&File"));
    MenuItem menuItem = menu.add(new MenuItem("About &Cute4j"));
    menuItem.setStatusTipText("Displays information about Cute4j.");
    menuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Swing.about();
      }
    });
    menu.add(new MenuItem(new AboutQtAction()));
    menu.addSeparator();
    menuItem = menu.add(new MenuItem("&Open"));
    menuItem.setStatusTipText("Disabled menu item.");
    menuItem.setEnabled(false);
    menuItem = menu.add(new MenuItem("&Save"));
    menuItem.setEnabled(false);
    menuItem = menu.add(new MenuItem("Save &As..."));
    menuItem.setEnabled(false);
    menu.addSeparator();
    menuItem = menu.add(new MenuItem("E&xit"));
    menuItem.setStatusTipText("Exits demonstration.");
    menuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Swing.stopEventLoop();
      }
    });
    menu = menuBar.add(new Menu("&View"));
    CheckBoxMenuItem checkBoxMenuItem = new CheckBoxMenuItem("&Tool Bar");
    checkBoxMenuItem.setChecked(true);
    checkBoxMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        performToolBar();
      }
    });
    menu.add(checkBoxMenuItem);
    checkBoxMenuItem = new CheckBoxMenuItem("&Status Bar");
    checkBoxMenuItem.setChecked(true);
    checkBoxMenuItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        performStatusBar();
      }
    });
    menu.add(checkBoxMenuItem);
    menuBar.add(new StyleMenu());
    menuBar.add(new ThemeMenu());
    setMenuBar(menuBar);
  }

  private void buildToolBar() {
    toolBar = addToolBar(new ToolBar("Demos"));
  }

  private void buildContent() {
    tabbedPane = new TabbedPane();
    demoPanel = new Container(new BorderLayout());
    demoPanel.setInsets(1, 1, 1, 1);
    sourceCode = new CodeViewer();
    tabbedPane.addTab("Demo", demoPanel);
    tabbedPane.addTab("Source Code", sourceCode);
    add(tabbedPane);
  }

  private void buildStatusBar() {
    statusBar = setStatusBar(new StatusBar());
  }

  private void addDemo(DemoModule demo) {
    demos.add(demo);
    ToggleToolButton demoSelector = new ToggleToolButton(demo.getIcon());
    demoSelector.setToolTipText(demo.getName());
    demoSelector.setStatusTipText(demo.getDescription());
    demoSelector.addActionListener(new DemoSelectAction(demos.size() - 1));
    toolBar.add(demoSelector);
  }

  private void addDemos() {
    demos = new ArrayList(17);
    SplashScreen.update("Adding InternalFrames Demo");
    addDemo(new InternalFramesDemo());
    SplashScreen.update("Adding Buttons Demo");
    addDemo(new ButtonsDemo());
    SplashScreen.update("Adding Dialogs Demo");
    addDemo(new DialogsDemo());
    SplashScreen.update("Adding ProgressBar Demo");
    addDemo(new ProgressBarDemo());
    SplashScreen.update("Adding ScrollPane Demo");
    addDemo(new ScrollPaneDemo());
    SplashScreen.update("Adding Slider Demo");
    addDemo(new SliderDemo());
    SplashScreen.update("Adding SplitPane Demo");
    addDemo(new SplitPaneDemo());
    SplashScreen.update("Adding TabbedPane Demo");
    addDemo(new TabbedPaneDemo());
    SplashScreen.update("Adding ToolTip Demo");
    addDemo(new ToolTipDemo());
    SplashScreen.update("Adding Tree Demo");
    addDemo(new TreeDemo());
  }

// Inner Classes. --------------------------------------------------------------

  /**
   * Demo Select Action.
   */
  private final class DemoSelectAction implements ActionListener {
    private final int demoIndex;
    DemoSelectAction(int demoIndex) {
      this.demoIndex = demoIndex;
    }
    public void actionPerformed(ActionEvent e) {
      setCurrentDemo(demoIndex);
    }
  }

}
